home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / NUMBERS.SWG / 0015_RANDOM1.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  2KB  |  54 lines

  1. {Another method to acComplish this (which only requires an order of n
  2. itterations is to generate an Array initialized from 2 to 1000 and then
  3. randomize that Array.  For your 400 numbers, just take 400 values in the
  4. new sequence (starting at the index of your lowest number). You can do
  5. that as follows:
  6. }
  7.  
  8. Const MaxNumber = 2000;
  9. Type SeqArray = Array [1..MaxNumber] of Integer;
  10.  
  11. {================================================================}
  12. Procedure RandomizeSeq (first, last: Integer; Var iseq: SeqArray);
  13. {================================================================}
  14.  
  15. Var           i, iran,
  16.            temp, imax : Integer;
  17.                     r : Real;
  18. {
  19.   Operation:  A random number within the range 1..last is generated
  20.   on each pass and the upper limit of the random number generated is
  21.   decreased by 1.  The value stored at the highest index of the last
  22.   pass is moved to the location of the last number selected.
  23.  
  24.   Parameters:
  25.     first = lowest number in sequence.
  26.      last = highest number in sequence.
  27.      iseq = Sequence Array
  28. }
  29. begin
  30.    { initialize sequence Array }
  31.    For i := first to last do iseq[i] := i;
  32.    Randomize;
  33.    { randomize the sorted Array }
  34.    For imax := last downto first do begin
  35.       { get a random number between 0 and 1 and scale up to
  36.         an Integer in the range of first to last }
  37.       r := random;
  38.       iran := Trunc(r*imax) + first;
  39.       { replace With value at highest index }
  40.       temp := iseq[iran];
  41.       iseq[iran] := iseq[imax];
  42.       iseq[imax] := temp
  43.    end;
  44. end;
  45.  
  46. { Example of generating 20 random numbers from 2 to 100: }
  47.  
  48. Var i : Integer;
  49.     a : SeqArray;
  50. begin
  51.    RandomizeSeq(2,100,a);
  52.    For i := 2 to 21 do Write(a[i]:3); Writeln;
  53. end.
  54.